﻿using System;
using System.Data;
using System.Windows.Forms;
using org.se.fs.presenter.IView.Profile;
using org.se.fs.presenter.View.Profile;

namespace org.se.fs.view.Profile
{
    public partial class FrmClientRecords : Form, IClientRecords
    {
        #region Public Property Variable
        public DataTable ProfileData { get; set; }
        public string Branch
        {
            get { return txtBranch.Text; }
            set { txtBranch.Text = value; }
        }
        public string Institution
        {
            get { return txtInstitution.Text; }
            set { txtInstitution.Text = value; }
        }
        public string Balance
        {
            get { return txtBalance.Text; }
            set { txtBalance.Text = value; }
        }
        public string NextPayment
        {
            get { return txtNextPayment.Text; }
            set { txtNextPayment.Text = value; }
        }
        public string PaymentDate
        {
            get { return txtPaymentDate.Text; }
            set { txtPaymentDate.Text = value; }
        }
        public bool EnableRegularLoan
        {
            get { return btnRegularLoan.Enabled; }
            set { btnRegularLoan.Enabled = value; }
        }
        public bool EnableBenefitsLoan
        {
            get { return btnBenefitsLoan.Enabled; }
            set { btnBenefitsLoan.Enabled = value; }
        }
        public bool EnableInstitution
        {
            get { return btnInstitution.Enabled; }
            set { btnInstitution.Enabled = value; }
        }
        public bool EnableNewPayment
        {
            get { return btnNewPayment.Enabled; }
            set { btnNewPayment.Enabled = value; }
        }
        public void DisplayLoans(DataTable data)
        {
            dgvLoans.DataSource = data;
        }
        public void DisplayPayments(DataTable data)
        {
            dgvPayments.DataSource = data;
        }
        #endregion

        private readonly ClientRecords _presenter;

        public FrmClientRecords()
        {
            InitializeComponent();
            _presenter = new ClientRecords(this);
        }

        public static DataTable FormShow(DataTable data)
        {
            using (var frmForm = new FrmClientRecords())
            {
                frmForm.ProfileData = data;
                frmForm.ShowDialog();
                return frmForm.ProfileData;
            }
        }

        private void BtnCloseClick(object sender, EventArgs e)
        {
            Close();
        }

        private void BtnUpdateClick(object sender, EventArgs e)
        {
            var data = FrmClientSubMenu.FormShow(ProfileData);
            if (data == null)
                return;
            ProfileData = data;
        }

        private void FrmFamilyBackgroundSubMenuLoad(object sender, EventArgs e)
        {
            var dr = ProfileData.Rows[0];
            label.Text = string.Format("{0}, {1} {2}.", dr["last_name"], dr["first_name"], dr["last_name"].ToString().Substring(0, 1));
            _presenter.Initialize();
        }

        private void TxtEnter(object sender, EventArgs e)
        {
            Focus();
        }

        private void TxtMouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Default;
        }

        private void BtnViewAllInstitutionClick(object sender, EventArgs e)
        {
            FrmClientInstitutionRecords.FormShow(_presenter.GetClientInstitutionData());
        }

        private void BtnInstitutionClick(object sender, EventArgs e)
        {
            if(btnRegularLoan.Enabled)
            {
                var result = MessageBox.Show(@"Are you sure you want to create new institution for the clent?",
                                             @"Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result.Equals(DialogResult.No))
                    return;
            }

            var data = FrmClientInstitutionSubMenu.FormShow(Convert.ToUInt64(ProfileData.Rows[0]["id"].ToString()));
            if (data == null)
                return;

            _presenter.GetClientInstitutionRecord();
        }

        private void btnRegularLoan_Click(object sender, EventArgs e)
        {
        }
    }
}